package com.bitsbazaar.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.bitsbazaar.entity.Product;
import com.bitsbazaar.entity.User;
import com.bitsbazaar.manager.ProductManager;
import com.bitsbazaar.manager.UserManager;

@Controller
public class HomeController {

	@Autowired
	private UserManager UserManager;
	@Autowired
	private ProductManager ProductManager;

	@RequestMapping(value = "/aboutUs", method = RequestMethod.GET)
	public ModelAndView aboutUs() {
		String message = "About us";
		return new ModelAndView("about", "message", message);
	}

	@RequestMapping(value = "/contact", method = RequestMethod.GET)
	public ModelAndView contact() {
		String message = "Contact";
		System.out.println(message);
		return new ModelAndView("contact", "message", message);
	}

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public ModelAndView index() {
		String message = "Home";
		System.out.println(message);
		return new ModelAndView("home", "message", message);
	}

	@RequestMapping(value = "/indexHome", method = RequestMethod.GET)
	public ModelAndView indexHome() {
		String message = "indexHome";
		System.out.println(message);
		return new ModelAndView("indexHome", "message", message);
	}

	@RequestMapping(value = "/signUp", method = RequestMethod.GET)
	public ModelAndView signUp() {
		String message = "Sing Up ";
		System.out.println(message);
		return new ModelAndView("signUp", "command", new User());
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		String message = "Entered Login ";
		System.out.println(message);
		return new ModelAndView("login", "command", new User());
	}

	@RequestMapping(value = "/registerUser", method = RequestMethod.POST)
	public ModelAndView registerUser(User user, HttpServletRequest request) {
		user.setType("user");
		Long status = UserManager.registerUser(user);
		if (Long.compare(status, new Long(-2)) != 0) {
			request.setAttribute("signUpSuccess", "Hey " + user.getName()	+ "! Thanks for signing up. Log In to Continue");
			User user2 = new User();
			user2.setEmail(user.getEmail());
			return new ModelAndView("login", "command", user2);
		} else {
			request.setAttribute("signUpError",	"Oh Snap! Your email address is already taken");
			user.setEmail("");
			return new ModelAndView("signUp", "command", user);
		}
	}

	@RequestMapping(value = "/authUser", method = RequestMethod.POST)
	public ModelAndView authUser(User user, HttpServletRequest request) {
		String message = "Entered authUser";
		System.out.println(message + user.getName() + " " + user.getEmail());
		Boolean status = UserManager.userAuthenticateByEmail(user.getEmail(), user.getPassword());
		System.out.println(user.getName() + "  : " + status);
		if (status) {
			user = UserManager.getUserByEmail(user.getEmail());
			HttpSession session = request.getSession(true);
			session.setAttribute("username", user.getName());
			session.setAttribute("userObject", user);
			String subs = (String) request.getSession().getAttribute("signUpBeforeSell");
			
			System.out.println(" : signUpBeforeSell :  " + subs);
			return new ModelAndView("home");
		} else {
			User userNew = new User();
			userNew.setEmail(user.getEmail());
			request.setAttribute("loginError", "UserName/Password did not matched.Try Again.");
			request.setAttribute("errorJs", "loginAgain");
			return new ModelAndView("login", "command", userNew);
		}
	}
	
	@RequestMapping(value = "/authAdmin", method = RequestMethod.POST)
	public ModelAndView authAdmin( User user, HttpServletRequest request) {
		String message = "Entered authAdmin";
		System.out.println(message + user + " " + ProductManager);
		Boolean status = ProductManager.adminAuthenticateByEmail(user.getEmail(), user.getPassword());
		System.out.println(message + user.getPassword() + " " + user.getEmail() + " " + status);
		System.out.println(user.getName() + "  : " + status);
		if (status) {
			System.out.println("admin auth --- good");
			user = UserManager.getUserByEmail(user.getEmail());
			HttpSession session = request.getSession(true);
			session.setAttribute("username", "Hey ! Admin, " + user.getName());
			session.setAttribute("userObject", user);
			session.setAttribute("admin", "true");
			String subs = (String) request.getSession().getAttribute("signUpBeforeSell");
			System.out.println(" : signUpBeforeSell :  " + subs);
			return new ModelAndView("home");
		} else {
			System.out.println("admin auth --- bad");
			User userNew = new User();
			userNew.setEmail(user.getEmail());
			request.setAttribute("loginError", "UserName/Password did not matched.Try Again.");
			request.setAttribute("errorJs", "loginAgain");
			return new ModelAndView("Admin", "command", userNew);
		} 
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request, WebRequest webRequest, SessionStatus status,	ModelAndView modelAndView) {
		String message = "Entered logout ";
		System.out.println(message);
		modelAndView.setViewName("home");
		System.out.println("before"	+ request.getSession().getAttribute("username"));
		request.getSession().removeAttribute("username");
		request.getSession().removeAttribute("userObject");
		request.getSession().removeAttribute("signUpBeforeSell");
		request.getSession().invalidate();
		System.out.println("after" + request.getSession().getAttribute("username"));
		return modelAndView;
	}
	
	@RequestMapping(value = "/Admin", method = RequestMethod.GET)
	public ModelAndView Admin() {
		String message = "Entered Admin ";
		System.out.println(message);
		return new ModelAndView("Admin", "command", new User());
	}
	
	
	@RequestMapping(value = "/validateproducts", method = RequestMethod.POST)
	public ModelAndView validateproducts(HttpServletRequest request) {
		
		List<Product> products=ProductManager.getApprovedProducts(0);
		 ModelAndView mav = new ModelAndView("validateproducts","command",new Product()) ;
		 mav.addObject("products",products);
		
		return mav;
	
	}

	@RequestMapping(value = "/accept", method = RequestMethod.POST)
	public ModelAndView accpetproducts(Product product ,HttpServletRequest request) {
		
		
		System.out.println("productid"+product.getId() );
		Product productDB=ProductManager.getProductById(product.getId());
		productDB.setApproved(1);
		ProductManager.updateProduct(productDB);
		List<Product> products=ProductManager.getApprovedProducts(0);
		 ModelAndView mav = new ModelAndView("validateproducts","command",new Product()) ;
		 mav.addObject("products",products);	
		return mav;
		
	}
	@RequestMapping(value = "/decline", method = RequestMethod.POST)
	public ModelAndView declineproducts(Product product ,HttpServletRequest request) {
		
		
		System.out.println("productid"+product.getId() );
		Product productDB=ProductManager.getProductById(product.getId());
		productDB.setApproved(2);
		ProductManager.updateProduct(productDB);
		List<Product> products=ProductManager.getApprovedProducts(0);
		 ModelAndView mav = new ModelAndView("validateproducts","command",new Product()) ;
		 mav.addObject("products",products);	
		return mav;
		
	}
}

